//
//  ZqlWelcomVC.swift
//  ZqlSinaWeiBo
//
//  Created by 赵乾龙 on 16/2/19.
//  Copyright © 2016年 Mr.zhao. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class ZqlWelcomVC: UIViewController {
    
    //自定义根视图
    override func loadView() {
        view = UIImageView(image: UIImage(named: "ad_background"))
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setUpUI()
    }
    
    //动画效果一般不再viewDidLoad中执行
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    //设置页面信息
    private func setUpUI() {
        view.addSubview(iconImage)
        view.addSubview(welcomLable)
        
        //设置子空间的约束
        iconImage.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-160)
            make.size.equalTo(CGSizeMake(90, 90))
        }
        
        welcomLable.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconImage.snp_centerX)
            make.top.equalTo(iconImage.snp_bottom).offset(16)
        }
        
        //隐藏欢迎lable
        welcomLable.alpha = 0
        
        //设置用户头像
        iconImage.sd_setImageWithURL(ZqlAccountVM.shardAccountVM.headImageUrl)
        //设置头像为圆形
        iconImage.layer.cornerRadius = 45
        iconImage.layer.masksToBounds = true
    }
    
    
    //懒加载视图
    //用户头像
    private lazy var iconImage:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    //用户名
    private lazy var welcomLable:UILabel = UILabel(text: (ZqlAccountVM.shardAccountVM.userName ?? "") + "欢迎归来", fontSize: 18, textColor: UIColor.darkGrayColor())
    
    
    //头像的动画效d
    private func startAnimation(){
        
        //移动的偏移量
        let offset = -UIScreen.mainScreen().bounds.height + 160
        
        //重新设置约束
        self.iconImage.snp_updateConstraints { (make) -> Void in
            
            make.bottom.equalTo(self.view.snp_bottom).offset(offset)
        }
        
        //使用UIView的动画
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            //强制刷新界面
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                //显示欢迎lable
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    
                    self.welcomLable.alpha = 1
                    
                    }, completion: { (_) -> Void in
                     //欢迎界面显示完毕后切换根视图为main界面
//                        UIApplication.sharedApplication().keyWindow?.rootViewController = ZqlMainController()
                    //发送通知
                        NSNotificationCenter.defaultCenter().postNotificationName(KChoseRootVC, object: "welcom")
                        
                })
        }
        
    }
    
}
